<template>
  <div class="login">
    <div class="login-form">
      <!-- 使用el-form的表单验证 -->
      <el-form :model="ruleForm"
               status-icon
               :rules="rules"
               ref="ruleForm"
               class="demo-ruleForm">
        <el-form-item prop="name">
          <el-input type="text"
                    v-model="ruleForm.name"
                    placeholder="请输入管理员名称"></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input type="password"
                    v-model="ruleForm.pass"
                    placeholder="请输入管理员密码"></el-input>
        </el-form-item>
        <p class="forget-password">
          <router-link to="/portal/forget">忘记密码</router-link>
        </p>
        <el-form-item>
          <el-button class="login-btn"
                     type="primary"
                     @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button class="login-btn"
                     @click="$router.push({path:'/register'})">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapMutations } from 'vuex';
export default {
  data () {
    return {
      ruleForm: {
        name: '',
        pass: '',
      },
      // rules中单个表单域校验规则可以包含多个数组，每个数组即是一条校验规则：
      rules: {
        name: [
          { required: true, message: '请输入管理员名称', trigger: 'blur' },
        ],
        pass: [
          { required: true, message: '请输入管理员密码', trigger: 'blur' },
          { min: 6, max: 6, message: '请输入6位密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    ...mapMutations(['setUser']),
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let user = {
            name: 'chenlongnian',
            pawd: '123456',
          };
          //提交mutations
          this.setUser(user);
          this.$router.push({ path: '/' });
        } else {
          return false;
        }
      });
    },
  }
}
</script>
<style scoped lang="scss">
.login {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: url("../../static/backpic.jpg") no-repeat;
  background-size: cover;
  .login-form {
    position: absolute;
    right: 0px;
    left: 0;
    bottom: 0;
    top: 0px;
    margin: auto;
    width: 250px;
    height: 250px;
    border-radius: 20px;
    padding: 60px 30px 20px 30px;
    background: rgba(225, 225, 225, 0.5);
    .forget-password {
      text-align: right;
      margin: -10px 10px 10px 10px;
      font-size: 12px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
